package
{
	import com.lang.IndexLang;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class Index extends Sprite
	{
		public var args:Object;
		
		private var _textField :TextField;
		private var _effectLoader :Loader;
		private var _mainLoader :Loader;
		
		public function Index()
		{
			args = {};
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		private function addedToStage( event :Event ) :void {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			_textField = new TextField();
			_textField.autoSize = TextFieldAutoSize.LEFT;
			_textField.text = IndexLang.INIT;
			_textField.textColor = 16777215;
			_textField.selectable = false;
			
			addChild( _textField );
			
			var resoucesPathPrefix :String = args["client_url"] || Config.RESOURCES_PATH_PREFIX;
			
			_effectLoader = new Loader();
			_effectLoader.load( new URLRequest( resoucesPathPrefix + "assets/init_effect.swf" ) );
			addChild( _effectLoader );
			
			var versionInfo :String = "?";
			versionInfo += "rnd=" + args["rnd"];
			
			_mainLoader = new Loader();
			_mainLoader.load( new URLRequest( resoucesPathPrefix + "Main.swf" + versionInfo ) );
			
			addEvents();
			
			center();
			
			stage.addEventListener(Event.RESIZE, center);
		}
		
		private function addEvents() :void {
			_mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, ch);
			
			_effectLoader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, hsh);
			_effectLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ieh);
			_effectLoader.contentLoaderInfo.addEventListener(Event.INIT, ih);
			_effectLoader.contentLoaderInfo.addEventListener(Event.OPEN, oh);
			_effectLoader.contentLoaderInfo.addEventListener(Event.UNLOAD, uh);
			_effectLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, ph);
		}
		
		private function removeEvents() :void {
			_mainLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, ch);
			
			_effectLoader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, hsh);
			_effectLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ieh);
			_effectLoader.contentLoaderInfo.removeEventListener(Event.INIT, ih);
			_effectLoader.contentLoaderInfo.removeEventListener(Event.OPEN, oh);
			_effectLoader.contentLoaderInfo.removeEventListener(Event.UNLOAD, uh);
			_effectLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, ph);
		}
		
		private function ch( event :Event ) :void {
			removeEvents();
			stage.removeEventListener(Event.RESIZE, center);
			stage.addChild( _mainLoader.content );
			_mainLoader = null;
		}
		
		private function hsh( event :HTTPStatusEvent ) :void {
			
		}
		
		private function ieh( event :IOErrorEvent ) :void {
			
		}
		
		private function ih( event :Event ) :void {
			
		}
		
		private function oh( event :Event ) :void {
			
		}
		
		private function uh( event :Event ) :void {
			
		}
		
		private function ph( event :ProgressEvent ) :void {
			
		}
		
		private function center( event :Event = null ) :void {
			_textField.x = (stage.stageWidth - _textField.width) / 2;
			_textField.y = (stage.stageHeight - _textField.height) / 2;
			_effectLoader.x = (stage.stageWidth - 40) / 2;
			_effectLoader.y = _textField.y - 40 - 5;
		}
	}
}